<template>
  <div>
    <div class="node" v-has="{action: 'coupon:list', effect: 'move'}">
    <div class="search_box topSelect">
      
      <div class="searchBox1" style="margin-left: 20px">
        <el-button size="small" plain type="primary" icon="el-icon-search" @click="getQueryData">查询</el-button>     
      </div>
    </div>
    <Panel title="积分活动" :intro="intro">
      <div slot='ext' class="examine">
        <el-button v-has="{action: 'coupon:add', effect: 'move'}" class="button-offt" type="success" icon="el-icon-plus" @click="()=>editStore()">新增</el-button>
    </div>
    <TableEncapsulation
      slot="body"
      :tableConfig="tableConfig"
      :tableData="tableData"
    >
    <template slot="operation" slot-scope="operation">
          <el-button size="mini" type="primary" 
            v-has="{action: 'scanCodeActivity:update', effect: 'move'}"
            v-if="operation.row.state==1"
            @click="()=>updateState(operation.row.id, 2)"
          >一键上线</el-button>
          <el-button size="mini" type="danger" icon="el-icon-close"
            v-has="{action: 'scanCodeActivity:update', effect: 'move'}"
            v-if="operation.row.state==2"
            @click="()=>updateState(operation.row.id, 1)"
          >一键下线</el-button>
        </template>
        <TablePager
          slot="pager"
          v-if="pager.use"
          :size="pager.size"
          :pageChange="pageChange"
          :show-total="pager.showTotal"
          :total="pager.total"
          :current="pager.current"
          layout="sizes, prev, pager, next"
        >
          <span class="demonstration">GO></span>
        </TablePager>
    </TableEncapsulation>
    </Panel>
    <!-- 添加编辑 start-->
    <ModalPanel 
      :visible='visible'
      title="添加/编辑"
      :showFooter='true'
      :onCancel="onCancel"
      class="bannerDialog"
      :onOk="onSubmit">
      <div>
        <ModifyForm
          :hiddenBtn='true'
          :editColumn='editColumn'
          :updateData="updateData"
          labelWidth='100px'
          ref="configModal"
          class="ModifyForm"
          :temporaryData='modalData'>
        <div slot="siteBoxSlot">
          <el-checkbox-group v-model="checkList">
            <el-checkbox  v-for="(item,index) in operatorSns" :key="index" :label="item.operatorSn+''">{{item.operatorName}}</el-checkbox>
          </el-checkbox-group>
        </div>
        </ModifyForm>
      </div>
    </ModalPanel>
    <!-- 添加编辑end -->
    </div>
    <div style="display: none;width:100%;height:100%;margin: auto;" v-has="{action: 'coupon:list', effect: 'noPremiss'}">
      <NoPremission></NoPremission>
    </div>
  </div>
</template>
<script>
import request from "@/tools/request";
import Panel from "@/components/Panel";
import TableEncapsulation from "@/components/TableEncapsulation";
import TablePager from "@/components/Pager";
import { objectFilter } from "@/tools/array";
import ModifyForm from "@/components/ModifyForm";
import ModalPanel from "@/components/ModalPanel";
import { dateFormat } from "@/tools/time";
export default {
  name: "couponList1",
  components: {
    TableEncapsulation,
    TablePager,
    Panel,
    ModifyForm,
    ModalPanel,
  },
  data() {
    return {
      intro:{
        title: '',
        content: ``
      },
      tableData: [],
      pager: {
        component: 'score',
        use: true,
        total: 0,
        current: 1,
        size: 15,
        showTotal: true
      },
      operation: true,
      //添加编辑
      operatorSns:[],
      visible:false,
      modalData:{},
      siteList: [],
      checkList: [],
      typesList:[],
      editColumn: [
        {
          name: "单位积分/元",
          prop: "unitScore",
          type: "number",
          width:24,
          required: true
        },
        {
        name: "指定运营商",
        prop: "ids",
        type: "other",
        width:24,
        required: false,
        slotName: 'siteBoxSlot',
        },
      ],
      //添加编辑end
    };
  },
  computed: {
    tableConfig () {
      return {
        selection: true,
        columnConfig: [
          {
            label: "编号",
            type: "text",
            prop: "id",
            align: "center",
            width: '50px'
          },
          {
            label: "单位积分(/元)",
            type: "text",
            prop: "unitScore",
            align: "center",
          },
          {
            label: "状态",
            type: "other",
            prop: "state",
            align: "center",
            fnc: val => val == 1 ? "下线" :"已上线",
            class:  val => val == 1 ? "cancel" :  "agree" ,
          },
          {
            label: "指定运营商",
            type: "text",
            prop: "operatorNames",
            align: "center"
          },
          this.operation && {
            type: "operation",
            align: "center",
            label: "操作",
            width:180
          }
        ].filter(Boolean)
        }
    }
  },
  mounted() {
    this.getQueryData();
    this.getSn()
    
  },
  methods: {
    async getSn() {
      const res = await request({
        url: `/manage/operator/select`,
        method: "POST",
      });
      this.operatorSns = res.data
    },
    async getQueryData() {
      let current =  this.pager.current
      let size = this.pager.size
      let search = objectFilter({ size, current })
      const res = await request({
        url: `/manage/score/activity/query`,
        method: "POST",
        data: search
      });
      let data = res.data.records
      data.map(item => {
       
      })
      this.tableData = data
      this.pager.total = res.data.total
      this.pager.current = res.data.current
    },
    pageChange(val) {
      this.pager.current = val;
      this.getQueryData();
    },
    changeSearch(key, val) {
      this.searchs[key] = val;
      this.pager.current = 1;
    },
    commentDetail(id){
      this.$store.commit('saveSearchs', this.searchs)
      this.$store.commit('savePager', this.pager)
      this.$router.push({ path: "/user/coupon/list",query:{couponId:id,type: "toview"} })
    },
    //添加编辑start
    editStore(rowData,state){
      if(rowData){
        if(rowData.stationIds){
          rowData.type1 = 2
          this.typesList = this.siteList
          var data = rowData.stationIds.split('|')
          this.checkList = data
          this.editColumn[3].hidden = false
          this.editColumn[2].hidden = true
        }
        this.modalData = {...rowData}
      }else{
        this.modalData ={}
      }
      this.visible = true
    },
    async onSubmit(){
      this.$refs['configModal'].onSubmit()
    },
    onCancel(){
      this.showSearch = false
      this.visible = false
      this.checkList =[]
    },
    async updateData(values){
      let operatorSns= this.checkList
      if(operatorSns.length<1){
        this.$message.error('请选择可使用运营商')
        return
      }
       let {id, unitScore } = values
      let res = await request({
        url: '/manage/score/activity/add',
        method: "POST",
        data:{id, unitScore ,operatorSns }
      })
     if(res.code !==200) return
      this.$message.success('成功')
      this.visible = false
      this.getQueryData();
    },
    //添加编辑end
   
   //updateState
   async updateState(id, state){
    let res = await request({
        url: '/manage/score/activity/update/state',
        method: "POST",
        data:{id, state}
      })
     if(res.code !==200) return
      this.$message.success('成功')
      this.getQueryData();
   },
  }
}
</script>
<style lang='scss' >

</style>